//
//  selectLabelView.m
//  Assistant
//
//  Created by wyopenminded on 2017/4/20.
//  Copyright © 2017年 秦慕乔. All rights reserved.
//

#import "selectLabelView.h"

@interface selectLabelView ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (strong, nonatomic) UICollectionView *collectionView;
@property (strong, nonatomic) NSMutableArray *dataArray;
@property (strong, nonatomic) UIButton *btn;
@end

@implementation selectLabelView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        _dataArray = [NSMutableArray array];
        _btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [self initializeSubviews];
    }
    return self;
}


- (void)initializeSubviews{
    [self addSubview:({
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumLineSpacing = 5;
        flowLayout.minimumInteritemSpacing = 5;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.size.width, self.size.height) collectionViewLayout:flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView setBackgroundColor:[UIColor clearColor]];
        [self addSubview:_collectionView];
        //注册
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"labelCell"];
        _collectionView;
    })];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArray.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat width = [self widthForLabel:_dataArray[indexPath.row] fontSize:14];
    return CGSizeMake(width+50,30);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"labelCell" forIndexPath:indexPath];
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, 0, ([self widthForLabel:_dataArray[indexPath.row] fontSize:14]+50), 30);
    [button setTitle:[NSString stringWithFormat:@"%@", _dataArray[indexPath.row]] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:14.0];
    button.backgroundColor = [UIColor colorWithHex:0xf2f2f2];
    button.layer.cornerRadius = 2.0;
    button.layer.masksToBounds = YES;
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    [button setTitleColor:RGB(101, 101, 101) forState:UIControlStateNormal];
    [button addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:button];
    
    return cell;
}

- (void)BtnClick:(UIButton *)btn
{
    self.searchBtnclick(btn.titleLabel.text);
    NSLog(@"%@", btn.titleLabel.text);
}

// 设置cell的水平间距
- (CGFloat) collectionView:(UICollectionView *)collectionView
                    layout:(UICollectionViewLayout *)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}

// 设置cell的竖直间距
- (CGFloat) collectionView:(UICollectionView *)collectionView
                    layout:(UICollectionViewLayout *)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}

//定义每个UICollectionView 的 margin(边缘，范围)
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 20, 0, 20);
}

/**
 *  计算文字长度
 */
- (CGFloat)widthForLabel:(NSString *)text fontSize:(CGFloat)font
{
    CGSize size = [text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:font], NSFontAttributeName, nil]];
    return size.width;
}

- (void)reloadData:(NSMutableArray* )array{
    CGRect frame = _collectionView.frame;
    frame.size.width = self.size.width;
    frame.size.height = self.size.height;
    _collectionView.frame = frame;
    _dataArray = array;
    [_collectionView reloadData];
}

- (void)addLabel{
//    EditPersonalViewController *vc = _vc;
//    if ([_type isEqualToString:@"职业标签"]) {
//        _btn.tag = 555;
//    }else{
//        _btn.tag = 666;
//    }
//    [vc addLabels:_btn];
}



@end
